/*     */ package com.zimbra.cs.store.triton;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.store.Blob;
/*     */ import com.zimbra.cs.store.BlobBuilder;
/*     */ import com.zimbra.cs.store.external.ExternalResumableIncomingBlob;
/*     */ import com.zimbra.cs.store.external.ExternalResumableOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.security.MessageDigest;
/*     */ import java.util.concurrent.atomic.AtomicLong;
/*     */ import org.apache.commons.codec.binary.Hex;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.HeadMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TritonIncomingBlob
/*     */   extends ExternalResumableIncomingBlob
/*     */ {
/*     */   private final String baseUrl;
/*     */   private final MozyServerToken serverToken;
/*     */   private final TritonUploadUrl uploadUrl;
/*     */   private final TritonBlobStoreManager.HashType hashType;
/*     */   private final MessageDigest digest;
/*     */   private TritonIncomingOutputStream outStream;
/*     */   private AtomicLong written;
/*     */   
/*     */   public TritonIncomingBlob(String id, String baseUrl, BlobBuilder blobBuilder, Object ctx, MessageDigest digest, TritonBlobStoreManager.HashType hashType)
/*     */     throws ServiceException, IOException
/*     */   {
/*  52 */     super(id, blobBuilder, ctx);
/*  53 */     this.digest = digest;
/*  54 */     this.hashType = hashType;
/*  55 */     this.baseUrl = baseUrl;
/*  56 */     this.serverToken = new MozyServerToken();
/*  57 */     this.uploadUrl = new TritonUploadUrl();
/*  58 */     this.written = new AtomicLong(0L);
/*     */   }
/*     */   
/*     */ 
/*     */   protected ExternalResumableOutputStream getAppendingOutputStream(BlobBuilder blobBuilder)
/*     */   {
/*  64 */     this.lastAccessTime = System.currentTimeMillis();
/*  65 */     this.outStream = new TritonIncomingOutputStream(blobBuilder, this.digest, this.hashType, this.baseUrl, this.uploadUrl, this.serverToken, this.written);
/*  66 */     return this.outStream;
/*     */   }
/*     */   
/*     */   public Blob getBlob() throws IOException, ServiceException
/*     */   {
/*  71 */     if (this.outStream != null) {
/*  72 */       this.outStream.close();
/*     */     }
/*  74 */     String locator = Hex.encodeHexString(this.digest.digest());
/*  75 */     return new TritonBlob(this.blobBuilder.finish(), locator, this.uploadUrl.toString(), this.serverToken);
/*     */   }
/*     */   
/*     */   protected long getRemoteSize() throws IOException
/*     */   {
/*  80 */     this.outStream.flush();
/*  81 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  82 */     HeadMethod head = new HeadMethod(this.baseUrl + this.uploadUrl);
/*  83 */     ZimbraLog.store.info("heading %s", new Object[] { head.getURI() });
/*     */     try {
/*  85 */       head.addRequestHeader("X-Mozy-Server-Token", this.serverToken.getToken());
/*  86 */       int statusCode = HttpClientUtil.executeMethod(client, head);
/*  87 */       if (statusCode == 200) {
/*  88 */         String contentLength = head.getResponseHeader("Content-Length").getValue();
/*  89 */         long remoteSize = -1L;
/*     */         try {
/*  91 */           remoteSize = Long.valueOf(contentLength).longValue();
/*     */         } catch (NumberFormatException nfe) {
/*  93 */           throw new IOException("Content length can't be parsed to Long", nfe);
/*     */         }
/*  95 */         return remoteSize;
/*     */       }
/*  97 */       ZimbraLog.store.error("failed with code %d response: %s", new Object[] { Integer.valueOf(statusCode), head.getResponseBodyAsString() });
/*  98 */       throw new IOException("unable to head blob " + statusCode + ":" + head.getStatusText(), null);
/*     */     }
/*     */     finally {
/* 101 */       head.releaseConnection();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/store/triton/TritonIncomingBlob.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */